package com.blog.wu.myblog.service;

import com.blog.wu.myblog.base.BaseCrudService;
import com.blog.wu.myblog.dao.ArticleInfoMapper;
import com.blog.wu.myblog.entity.ArticleInfo;
import com.blog.wu.myblog.util.Const;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author wuyanshen
 * @Date 2018-10-24 1:43 PM
 * @Discription 文章信息业务类
 */
@Service
public class ArticleInfoService extends BaseCrudService<ArticleInfoMapper, ArticleInfo> {

    /**
     * 将图片上传到图片服务器
     *
     * @param file
     * @return java.lang.String
     * @Date 2018/10/31 5:09 PM
     * @Author wuyanshen
     */
    public String doPutFile(MultipartFile file) {

        try {
            String filename = file.getOriginalFilename();
            SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
            String date = format.format(new Date());
            String url = Const.FILE_URL + date + filename;

            //jersey客户端
            Client client = new Client();
            WebResource webResource = client.resource(url);
            //将文件转化为byte
            byte[] buf = file.getBytes();

            webResource.put(String.class, buf);

            logger.info("图片url: {}", url);

            return url;

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
